#include "TcpStatusWidget.h"
#include <QLabel>
#include <QLineEdit>
#include <QHBoxLayout>
#include <QJsonObject>
#include <QGroupBox>  // 添加这个头文件来使用 QGroupBox
#include <QTimer>

// TcpStatusWidget* TcpStatusWidget::instance()
// {
//     static TcpStatusWidget instance;
//     return &instance;
// }

TcpStatusWidget::TcpStatusWidget(const QJsonObject& TcpToolPosition, QWidget* parent)
    : QWidget(parent) {

    // 创建一个垂直布局来容纳各个控制项
    QVBoxLayout* layout = new QVBoxLayout(this);

    layout->setContentsMargins(0,0,0,0);

    // 创建一个 QGroupBox 来容纳所有的控件
    QGroupBox* groupBox = new QGroupBox("TCP Status Information", this);  // 设置标题
    QVBoxLayout* groupBoxLayout = new QVBoxLayout(groupBox);  // 创建 GroupBox 内部的布局

    groupBoxLayout->setContentsMargins(5,5,5,5);

    // 遍历 TcpToolPosition 中的每个项（x, y, z, Rx, Ry, Rz）
    for (auto it = TcpToolPosition.begin(); it != TcpToolPosition.end(); ++it) {
        QString key = it.key();
        QString value = it.value().toString();
        TcpList.push_back(key);
        qDebug() << key + "111" << Qt::endl;
        // 跳过 uiElement
        if (key == "uiElement") continue;

        // 创建并显示对应的控件
        createTcpControl(key, value, groupBoxLayout);
    }

    // 将 groupBox 添加到外层的布局中
    layout->addWidget(groupBox);

    setLayout(layout);
    qDebug() << "测试tcpStatus："<<TcpList.size()<< Qt::endl;

    // 创建 QTimer 对象
    QTimer *timer = new QTimer(this);

    // 将 QTimer 的 timeout() 信号与 updateFrame() 槽函数连接
    connect(timer, &QTimer::timeout, this, &TcpStatusWidget::tcpStatusCallBack);

    // 启动定时器，设置间隔为 16 毫秒（大约 60 FPS）
    timer->start(16);
}

void TcpStatusWidget::updateTcp(const QString &label, const QString &value)
{
    if (valueLineEdits.contains(label)) {
        valueLineEdits[label]->setText(value);  // 更新位置 QLineEdit 的内容
    } else {
        qDebug() << "Tcp key not found: " << label;
    }
}

void TcpStatusWidget::createTcpControl(const QString& key, const QString& value, QVBoxLayout* layout) {
    // 创建一个 QHBoxLayout 来显示每个键值对
    QWidget* widget = new QWidget(this);
    QHBoxLayout* widgetLayout = new QHBoxLayout(widget);

    widgetLayout->setContentsMargins(0,0,0,0);

    widgetLayout->addItem(new QSpacerItem(20, 20, QSizePolicy::Expanding, QSizePolicy::Minimum));

    // 显示标签（如 x, y, z, Rx, Ry, Rz）
    QLabel* label = new QLabel(key + ":", widget);
    label->setMinimumSize(30, 15);

    widgetLayout->addWidget(label);

    // 添加弹簧
    widgetLayout->addItem(new QSpacerItem(20, 20, QSizePolicy::Expanding, QSizePolicy::Minimum));

    // 创建 QLineEdit 来显示值
    QLineEdit* valueLineEdit = new QLineEdit(value, widget);
    valueLineEdit->setReadOnly(true);  // 设置为只读
    widgetLayout->addWidget(valueLineEdit);
    // 添加弹簧
    widgetLayout->addItem(new QSpacerItem(20, 20, QSizePolicy::Expanding, QSizePolicy::Minimum));
    valueLineEdits[key] = valueLineEdit;

    // 将这个控件添加到布局中
    layout->addWidget(widget);
}


void TcpStatusWidget::tcpStatusCallBack()
{
    endPoseValue = mStatus.mContent["endPose"];
    if(endPoseValue.size()<6)
    {
        //std::cout<<"endPoseValue.size()<6"<<std::endl;
        return;
    }
    //std::cout<<"endPoseValue.size() = "<<endPoseValue.size()<<std::endl;
    if(TcpList.size()<6)
    {
        //std::cout<<"TcpList.size()<6"<<std::endl;
        //std::cout<<"TcpList.size() = "<<TcpList.size()<<std::endl;
        return;
    }

    // for(double num:endPoseValue)
    //     std::cout<<"endPose="<<num<<std::endl;
    // for(QString tcpO:TcpList)
    //     qDebug() << "TcpList=" << tcpO;

    for(int i=0;i<3;i++)
    {
        updateTcp(TcpList[i+3],QString::number(endPoseValue[i], 'f', 3));
    }

    for(int i=3;i<6;i++)
    {
        updateTcp(TcpList[i-3],QString::number(endPoseValue[i], 'f', 3));
    }

    // for(double var:endPoseValue)
    // {
    //     std::cout<<var<<std::endl;
    // }
}
